﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;



using Meqs.Entity;
using Meqs.Logic.Interface;

using Castle.Services.Transaction;
using Castle.Facilities.IBatisNetIntegration;
using log4net;

namespace Meqs.Logic.Implement
{
    public class StuCauseEvalTypeLogic : CommonLogic<StuCauseEvalType, int>, IStuCauseEvalTypeLogic
    {
        private ISchoolInfoLogic _schoolLogic;
        private IStuCauseEvalItemLogic _itemLogic;
        private IStuCauseEvalHistoryLogic _historyLogic;
        public StuCauseEvalTypeLogic(ISchoolInfoLogic schoolLogic,IStuCauseEvalItemLogic itemLogic,IStuCauseEvalHistoryLogic historyLogic)
        {
            _schoolLogic = schoolLogic;
            _itemLogic = itemLogic;
            _historyLogic = historyLogic;
        }
        #region Implementation of ICommonLogic<StuCauseEvalType, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(StuCauseEvalType entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(StuCauseEvalType entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public StuCauseEvalType FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<StuCauseEvalType> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<StuCauseEvalType> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion


        /// <summary>
        /// 验证类型是否被使用
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="historyId"></param>
        /// <param name="causeId"></param>
        /// <param name="typeName"></param>
        /// <returns>0-未使用，大于0-被使用</returns>
        public int ValidateTypeName(string schoolMark, int historyId, int causeId, string typeName)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("causeId", causeId);
            table.Add("historyId", historyId);
            table.Add("typeName", typeName);
            int result = 0;
            try
            {
                result = FindCustomerByCount("StuCauseEvalType.ValidateTypeName", table);
            }
            catch (Exception e)
            {
                throw;
            }

            return result;
        }

        /// <summary>
        /// 添加类型，包含自增序号信息
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="historyId"></param>
        /// <param name="causeId"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public int InsertType(string schoolMark, int historyId, int causeId, string typeName)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("causeId", causeId);
            table.Add("historyId", historyId);
            table.Add("typeName", typeName);
            int result = 0;
            try
            {
                result = (int)InsertCustomer("StuCauseEvalType.InsertType", table);
            }
            catch (Exception e)
            {
                throw;
            }

            return result;
        }


        /// <summary>
        /// 添加history，添加类型，包含自增序号信息
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="causeId"></param>
        /// <param name="typeName"></param>
        /// <param name="schoolYear"></param>
        /// <param name="term"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.Requires)]
        public int InsertTypeHistory(string schoolMark, int causeId, string typeName, int schoolYear, int term, int userId)
        {
            int result = 0;
            try
            {
                Hashtable table = new Hashtable();
                table.Add("SchoolMark", schoolMark);
                table.Add("SchoolYear", schoolYear);
                table.Add("Term", term);
                table.Add("CauseID", causeId);
                table.Add("Status", 0);
                table.Add("ManagerID", userId);

                int historyId =(int)InsertCustomer("StuCauseEvalHistory.InsertNew", table);
                if (historyId > 0)
                {
                    result = InsertType(schoolMark, historyId, causeId, typeName);
                }
                return historyId;
            }
            catch (Exception e)
            {
                return 0;       
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="typeId"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.Requires)]
        public int DeleteById(string schoolMark,int historyId,int typeId,int schoolYear,int term)
        {
            //删除项
            _itemLogic.DeleteByTypeId(schoolMark, typeId);

            //修改history的状态 改为0
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("Status", 0);
            _historyLogic.UpdateStatus(schoolMark, historyId, schoolYear, term,0);

            //删除类
            table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("RID", typeId);

            return DeleteCustomer("StuCauseEvalType.DeleteById", table);
        }

        /// <summary>
        /// 更改类型名
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="typeName"></param>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public int UpdateTypeName(string schoolMark, string typeName, int typeId)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("typeName", typeName);
            table.Add("RID", typeId);

            return UpdateCustomer("StuCauseEvalType.UpdateTypeName", table);
        }

        /// <summary>
        /// 获取所有学分评价大类(小类无大类的算做大类)
        /// </summary>
        /// <param name="causeID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<StuCauseEvalType> GetAllStuCauseEvalType(int causeID, string schoolMark)
        {
            SchoolInfo school = _schoolLogic.FindBySchoolMark(schoolMark);
            int schoolYear = school.SchoolYear;
            int term = school.SchoolTerm;
            return GetAllStuCauseEvalType(causeID, schoolMark, schoolYear, term);
        }

        /// <summary>
        /// 获取所有德育评价大类(小类无大类的算做大类)
        /// </summary>
        /// <param name="causeID"></param>
        /// <param name="schoolMark"></param>
        /// <param name="schoolYear"></param>
        /// <param name="term"></param>
        /// <returns></returns>
        public IList<StuCauseEvalType> GetAllStuCauseEvalType(int causeID, string schoolMark, int schoolYear, int term)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("SchoolYear", schoolYear);
            ht.Add("Term", term);
            ht.Add("CauseID", causeID);
            return FindCustomer<StuCauseEvalType>("StuCauseEvalType.GetAllStuCauseEvalType", ht);
        }
    }
}